﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using CosyTrade.ClientForm.Distribution;
using CosyTrade.ClientForm.Printer;
using CosyTrade.Financial;
using CosyTrade.Organization;
using CosyTrade.Trade;
using CosyTrade.UI;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using CosyTrade.Store;
using CosyTrade.ClientForm.Properties;

namespace CosyTrade.ClientForm.Trade
{
    public partial class Import_Shipping : FormBase
    {
        public Import_Shipping()
        {
            InitializeComponent();
        }
        private Image headimage;
        private Image bodyimage;
        private Font captionFont;
        private Color captionColor;
        private bool paintCaption;
        protected override void InitCommandBar()
        {
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("ComfirmRecBill", Resources.application_go, "提单签收", true, true, true,
                                                               ComfirmRecCallback));

            ToolbarItems.AddCommandItem("PrintExportBill", Resources.printer, "打印进口单", true, true, true, PrintExportCallback);

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("headAndbodySetting1", Resources.picture_add, "相簿封面与背景", true, true, true,
                                                               TemplateSettingCallBack));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("PrintCategoryBook1", Resources.printer_empty, "打印相簿", true, true, true,
                                                               PrintCategoryBookCallback));

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("ExportBillDetail", Resources.table_go, "出口单明细", true, true, true,
                                                               BillDetailCallback));

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("RefreshExportBill", Resources.table, "刷新列表", true, true, true,
                                                               RefreshCallback));

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("Close", Resources.door_out, "关闭", true, true, true,
                                                               CloseCallback));

        }

        private void PrintCategoryBookCallback(object sender, EventArgs e)
        {
            ImportBillBase importBill = bindingSource1.Current as ImportBillBase;
            if (importBill == null)
            {
                Functions.ShowMessageBox("请选择要打印的进口单");
                return;
            }
            IList<CategoryBookItem> bookitems = new List<CategoryBookItem>();
            ImportBill import = Operation.TradeSystem.GetImportBillDetail(importBill.PrimaryKey);
            if (import.BillItems != null)
            {
                foreach (ImportBillItem item in import.BillItems)
                {
                    
                    CompanyProductView view = new CompanyProductView();
                    ProductPrice pp = new ProductPrice();
                    pp.Price = item.Price;
                    pp.CompanyStore = item.MaptoExportBillItem.Product;
                    pp.ShortCode = item.MaptoExportBillItem.ShortCode;
                   
                    view.OnCompany = GlobalInfo.CurrentCompany;
                    view.PackageCount = item.ExportItem_PackageCount;
                    view.PackageUnitCount = item.PackageCount;
                    view.Price = pp;
                    view.Store = item.MaptoExportBillItem.Product;
                    view.TotalBulk = item.MaptoExportBillItem.TotalBulk;
                    view.TotalWeight = item.MaptoExportBillItem.TotalWeight;

                    CategoryBookItem books = new CategoryBookItem();
                    books.OrderKey = 0;
                    books.ProductView = view;
                    bookitems.Add(books);
                }
            }

            //if (currentObj == null) return;
            if (headimage == null || bodyimage == null)
                TemplateSettingCallBack(null, null);
            if (headimage == null) headimage = Properties.Resources.lifenghead;
            if (bodyimage == null) bodyimage = Properties.Resources.lifenglist;

            PrintProduct print = new PrintProduct();
            print.BodyImage = bodyimage;
            print.HeadImage = headimage;
            print.DataStorage = Operation.DataStorage;
            print.Exchange = importBill.ImportExchangeRate;
            print.PaintCaption = paintCaption;
            print.CaptionFont = captionFont;
            print.CaptionColor = captionColor;
            print.CompanyName = GlobalInfo.CurrentCompany.Caption;
            print.DrawchangdaProducts(bookitems, GlobalInfo.CurrentCompany);
        }

        private void TemplateSettingCallBack(object sender, EventArgs e)
        {
            CategoryBookPageSeting setting = new CategoryBookPageSeting();
            setting.ShowDialog();
            headimage = setting.HeadImage;
            bodyimage = setting.BodyImage;
            captionColor = setting.CaptionColor;
            captionFont = setting.CaptionFont;
            paintCaption = setting.PaintCaption;
            //if (!setting.PaintCaption) return;
            //Graphics g = Graphics.FromImage(headimage);
            //Brush b = new SolidBrush(setting.CaptionColor);
            //SizeF sf = PrintHelper.GetSizeF(GlobalInfo.CurrentCompany.Caption, setting.CaptionFont);
            //g.DrawString(GlobalInfo.CurrentCompany.Caption, setting.CaptionFont, b,
            //             new RectangleF((headimage.Width - sf.Width) / 2, (headimage.Height - sf.Height) / 2,sf.Width + 10, sf.Height));
        }


        private void PrintExportCallback(object sender, EventArgs e)
        {
            ImportBillBase importBill = bindingSource1.Current as ImportBillBase;
            if (importBill == null)
            {
                Functions.ShowMessageBox("请选择要打印的进口单");
                return;
            }
            PrintImport printBill = new PrintImport(importBill);
            printBill.ShowDialog();
        }

        private void ComfirmRecCallback(object sender, EventArgs e)
        {
            ExportBillBase curr = bindingSource1.Current as ExportBillBase;
            if(curr==null)
            {
                Functions.ShowMessageBox("请选择出口单");
                return;
            }

            ComfirmReciveExportBill comfirmForm = new ComfirmReciveExportBill(curr.ToCompany.PrimaryKey, curr.ConsignmentDate.Value);
            if (comfirmForm.ShowDialog() != DialogResult.OK) return;

            ImportBill importBill = Operation.TradeSystem.ReciveExportBill(curr.PrimaryKey, comfirmForm.ReciveDate,
                                                   comfirmForm.ReciveEmployee.PrimaryKey);
            bindingSource1.RemoveCurrent();

            ImportBillDetail billDetail = new ImportBillDetail(importBill);
            billDetail.Show();
        }

        private void BillDetailCallback(object sender, EventArgs e)
        {
            OpenCurrentDetail();
        }

        private void OpenCurrentDetail()
        {
            ExportBillBase curr = bindingSource1.Current as ExportBillBase;
            if(curr==null)
            {
                Functions.ShowMessageBox("请选择出口单");
                return;
            }
            ExportDetailForm form = new ExportDetailForm(curr.PrimaryKey);
            form.ReadOnlyMode = true;
            form.Show();
        }

        private void RefreshCallback(object sender, EventArgs e)
        {
            ((ThreadStart)LoadDataSourceInvoke).BeginInvoke(null, null);
        }

        private void CloseCallback(object sender, EventArgs e)
        {
            Close();
        }

        private void rptCompany_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            Company cmp = e.Value as Company;
            if (cmp != null) e.DisplayText = cmp.Caption;
        }

        private void rptEmployee_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            Employee emp = e.Value as Employee;
            if (emp != null) e.DisplayText = emp.EmpName;
        }

        private void rptRate_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            ExchangeRate rate = e.Value as ExchangeRate;
            if (rate != null) e.DisplayText = rate.Exchange.ToString();
        }

        private void Import_Shipping_Load(object sender, EventArgs e)
        {
            ((ThreadStart) LoadDataSourceInvoke).BeginInvoke(null, null);
        }

        private void LoadDataSourceInvoke()
        {
            IList<ExportBillBase> dataSource = new List<ExportBillBase>(
                Operation.TradeSystem.GetNewImportBillList(GlobalInfo.CurrentCompany.PrimaryKey));
            bindingSource1.DataSource = dataSource;
        }

        private void gridView1_KeyUp(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = false;
            if (e.KeyCode == Keys.Enter) OpenCurrentDetail();
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left && e.Clicks>=2)
            {
                GridHitInfo hitInfo = gridView1.CalcHitInfo(e.Location);
                if (hitInfo.InRow) OpenCurrentDetail();
            }
        }
    }
}
